/**
 *
 */
package cn.ipenta.jkb.web;

import cn.ipenta.jkb.entity.Healer;
import cn.ipenta.jkb.entity.Regulator;
import cn.ipenta.jkb.repository.HealerRepository;
import cn.ipenta.jkb.repository.RegulatorRepository;
import cn.ipenta.jkb.utils.JkbWebUtils;
import cn.ipenta.jkb.utils.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Map;

/**
 * @author ipenta
 * @ClassName: SupervisorController
 * @Description: TODO
 * @date 2017年4月16日 上午10:31:54
 */
@Controller
public class AdminController {

	@Autowired
	RegulatorRepository regulatorRepository;

	@Autowired
	HealerRepository healerRepository;

	@GetMapping("/")
	public String home() {
		return "redirect:/admin";
	}


	@GetMapping("/admin")
	public String index() {
		return "admin/index";
	}

	@GetMapping("/admin/regulator")
	public String regulator(Map<String, Object> map) {
		map.put("regulators", regulatorRepository.findAll());
		return "admin/regulator/index";
	}

	@GetMapping("/admin/regulator/form")
	public String formRegulator() {
		return "admin/regulator/form";
	}

	@PostMapping("/admin/regulator/form")
	public String submit(Regulator regulator) {
		regulator.setId(JkbWebUtils.getUUID());
		regulator.setPassword(RandomUtils.getRandomString(6));

		Regulator regul = regulatorRepository.save(regulator);

		if (regul != null) {
			return "redirect:/admin/regulator";
		}
		return null;
	}

	@GetMapping("/admin/healer")
	public String healer(Map<String, Object> map) {
		map.put("healers", healerRepository.findAll());
		return "admin/healer/index";
	}

	@GetMapping("/admin/healer/form")
	public String formhealer() {
		return "admin/healer/form";
	}

	@PostMapping("/admin/healer/form")
	public String submit(Healer healer) {
		healer.setId(JkbWebUtils.getUUID());
		healer.setPassword(RandomUtils.getRandomString(6));
		Healer entity = healerRepository.save(healer);
		if (entity != null) {
			return "redirect:/admin/healer";
		}
		return null;
	}
}
